package org.apache.james.mime4j.message;

import java.io.IOException;
import java.io.InputStream;
import java.util.Stack;
import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.decoder.Base64InputStream;
import org.apache.james.mime4j.decoder.QuotedPrintableInputStream;
import org.apache.james.mime4j.descriptor.BodyDescriptor;
import org.apache.james.mime4j.field.Field;
import org.apache.james.mime4j.parser.ContentHandler;
import org.apache.james.mime4j.util.CharArrayBuffer;

public class MessageBuilder
  implements ContentHandler
{
  static Class class$org$apache$james$mime4j$message$BodyPart;
  static Class class$org$apache$james$mime4j$message$Entity;
  static Class class$org$apache$james$mime4j$message$Header;
  static Class class$org$apache$james$mime4j$message$Message;
  static Class class$org$apache$james$mime4j$message$Multipart;
  private final Entity entity;
  private Stack stack = new Stack();

  public MessageBuilder(Entity paramEntity)
  {
    this.entity = paramEntity;
  }

  static Class class$(String paramString)
  {
    Class localClass;
    try
    {
      localClass = Class.forName(paramString);
      return localClass;
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      throw new NoClassDefFoundError().initCause(localClassNotFoundException);
    }
  }

  private void expect(Class paramClass)
  {
    if (!(paramClass.isInstance(this.stack.peek())))
      throw new IllegalStateException("Internal stack error: Expected '" + paramClass.getName() + "' found '" + this.stack.peek().getClass().getName() + "'");
  }

  public void body(BodyDescriptor paramBodyDescriptor, InputStream paramInputStream)
    throws MimeException, IOException
  {
    Class localClass;
    String str;
    Object localObject1;
    Object localObject2;
    if (class$org$apache$james$mime4j$message$Entity == null)
    {
      localClass = class$("org.apache.james.mime4j.message.Entity");
      class$org$apache$james$mime4j$message$Entity = localClass;
      expect(localClass);
      str = paramBodyDescriptor.getTransferEncoding();
      if (!("base64".equals(str)))
        break label103;
      localObject1 = new Base64InputStream(paramInputStream);
      if (!(paramBodyDescriptor.getMimeType().startsWith("text/")))
        break label132;
      localObject2 = new TempFileTextBody((InputStream)localObject1, paramBodyDescriptor.getCharset());
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            ((Entity)this.stack.peek()).setBody((Body)localObject2);
            return;
            localClass = class$org$apache$james$mime4j$message$Entity;
          }
          label103: if (!("quoted-printable".equals(str)))
            break;
          localObject1 = new QuotedPrintableInputStream(paramInputStream);
        }
        localObject1 = paramInputStream;
      }
      label132: localObject2 = new TempFileBinaryBody((InputStream)localObject1);
    }
  }

  public void endBodyPart()
    throws MimeException
  {
    Class localClass;
    if (class$org$apache$james$mime4j$message$BodyPart == null)
    {
      localClass = class$("org.apache.james.mime4j.message.BodyPart");
      class$org$apache$james$mime4j$message$BodyPart = localClass;
    }
    while (true)
    {
      expect(localClass);
      this.stack.pop();
      return;
      localClass = class$org$apache$james$mime4j$message$BodyPart;
    }
  }

  public void endHeader()
    throws MimeException
  {
    Class localClass1;
    Header localHeader;
    Class localClass2;
    if (class$org$apache$james$mime4j$message$Header == null)
    {
      localClass1 = class$("org.apache.james.mime4j.message.Header");
      class$org$apache$james$mime4j$message$Header = localClass1;
      expect(localClass1);
      localHeader = (Header)this.stack.pop();
      if (class$org$apache$james$mime4j$message$Entity != null)
        break label75;
      localClass2 = class$("org.apache.james.mime4j.message.Entity");
      class$org$apache$james$mime4j$message$Entity = localClass2;
    }
    while (true)
    {
      while (true)
      {
        expect(localClass2);
        ((Entity)this.stack.peek()).setHeader(localHeader);
        return;
        localClass1 = class$org$apache$james$mime4j$message$Header;
      }
      label75: localClass2 = class$org$apache$james$mime4j$message$Entity;
    }
  }

  public void endMessage()
    throws MimeException
  {
    Class localClass;
    if (class$org$apache$james$mime4j$message$Message == null)
    {
      localClass = class$("org.apache.james.mime4j.message.Message");
      class$org$apache$james$mime4j$message$Message = localClass;
    }
    while (true)
    {
      expect(localClass);
      this.stack.pop();
      return;
      localClass = class$org$apache$james$mime4j$message$Message;
    }
  }

  public void endMultipart()
    throws MimeException
  {
    this.stack.pop();
  }

  public void epilogue(InputStream paramInputStream)
    throws MimeException, IOException
  {
    Class localClass;
    CharArrayBuffer localCharArrayBuffer;
    if (class$org$apache$james$mime4j$message$Multipart == null)
    {
      localClass = class$("org.apache.james.mime4j.message.Multipart");
      class$org$apache$james$mime4j$message$Multipart = localClass;
    }
    while (true)
    {
      expect(localClass);
      localCharArrayBuffer = new CharArrayBuffer(128);
      while (true)
      {
        int i = paramInputStream.read();
        if (i == -1)
          break label62;
        localCharArrayBuffer.append((char)i);
      }
      localClass = class$org$apache$james$mime4j$message$Multipart;
    }
    label62: ((Multipart)this.stack.peek()).setEpilogue(localCharArrayBuffer.toString());
  }

  public void field(String paramString)
    throws MimeException
  {
    Class localClass;
    if (class$org$apache$james$mime4j$message$Header == null)
    {
      localClass = class$("org.apache.james.mime4j.message.Header");
      class$org$apache$james$mime4j$message$Header = localClass;
    }
    while (true)
    {
      expect(localClass);
      ((Header)this.stack.peek()).addField(Field.parse(paramString));
      return;
      localClass = class$org$apache$james$mime4j$message$Header;
    }
  }

  public void preamble(InputStream paramInputStream)
    throws MimeException, IOException
  {
    Class localClass;
    CharArrayBuffer localCharArrayBuffer;
    if (class$org$apache$james$mime4j$message$Multipart == null)
    {
      localClass = class$("org.apache.james.mime4j.message.Multipart");
      class$org$apache$james$mime4j$message$Multipart = localClass;
    }
    while (true)
    {
      expect(localClass);
      localCharArrayBuffer = new CharArrayBuffer(128);
      while (true)
      {
        int i = paramInputStream.read();
        if (i == -1)
          break label62;
        localCharArrayBuffer.append((char)i);
      }
      localClass = class$org$apache$james$mime4j$message$Multipart;
    }
    label62: ((Multipart)this.stack.peek()).setPreamble(localCharArrayBuffer.toString());
  }

  public void raw(InputStream paramInputStream)
    throws MimeException, IOException
  {
    throw new UnsupportedOperationException("Not supported");
  }

  public void startBodyPart()
    throws MimeException
  {
    Class localClass;
    if (class$org$apache$james$mime4j$message$Multipart == null)
    {
      localClass = class$("org.apache.james.mime4j.message.Multipart");
      class$org$apache$james$mime4j$message$Multipart = localClass;
    }
    while (true)
    {
      expect(localClass);
      BodyPart localBodyPart = new BodyPart();
      ((Multipart)this.stack.peek()).addBodyPart(localBodyPart);
      this.stack.push(localBodyPart);
      return;
      localClass = class$org$apache$james$mime4j$message$Multipart;
    }
  }

  public void startHeader()
    throws MimeException
  {
    this.stack.push(new Header());
  }

  public void startMessage()
    throws MimeException
  {
    Class localClass;
    if (this.stack.isEmpty())
    {
      this.stack.push(this.entity);
      return;
    }
    if (class$org$apache$james$mime4j$message$Entity == null)
    {
      localClass = class$("org.apache.james.mime4j.message.Entity");
      class$org$apache$james$mime4j$message$Entity = localClass;
    }
    while (true)
    {
      while (true)
      {
        expect(localClass);
        Message localMessage = new Message();
        ((Entity)this.stack.peek()).setBody(localMessage);
        this.stack.push(localMessage);
      }
      localClass = class$org$apache$james$mime4j$message$Entity;
    }
  }

  public void startMultipart(BodyDescriptor paramBodyDescriptor)
    throws MimeException
  {
    Class localClass;
    if (class$org$apache$james$mime4j$message$Entity == null)
    {
      localClass = class$("org.apache.james.mime4j.message.Entity");
      class$org$apache$james$mime4j$message$Entity = localClass;
    }
    while (true)
    {
      expect(localClass);
      Entity localEntity = (Entity)this.stack.peek();
      Multipart localMultipart = new Multipart(paramBodyDescriptor.getSubType());
      localEntity.setBody(localMultipart);
      this.stack.push(localMultipart);
      return;
      localClass = class$org$apache$james$mime4j$message$Entity;
    }
  }
}